/**
 * @file user_data.c
 * @author user data function
 * @brief 
 * @version 0.1
 * @date 2019-12-08
 * 
 * @copyright Copyright (c) 2019,CHIPSEA Co., Ltd. 
 * 
 */
#include "sdk_config.h"
#include "user_data.h"
#include "log.h"
#include "OSAL.h"
#include "flash.h"
#include "uart_process.h"


/**
 * @brief user_data
 * 
 */
user_data_t user_data;

/**
 * @brief bank_curr_num
 * 
 */
uint8 bank_curr_num = 0;

/**
 * @brief user_data_sum
 * 
 */
uint8 user_data_sum(uint16 len, uint8 *data)
{
    uint8 ret = 0;
    
    for(uint16 i=0; i<len; i++)
    {
        ret += data[i];
    }
    
    return ret;
}

/**
 * @brief user_data_init
 * 
 */
void user_data_init(void)
{
	uint8	buf[6];
	uint32	addr = 0;
	uint32	index1 = 0;
	uint32	index2 = 0;
	
	osal_memset(buf, 0, 6);
	osal_memset((uint8*)&user_data, 0, sizeof(user_data_t));
	
	HalFlashRead(BAUD_FLASH_ADDR1, buf, 6);
	if( (buf[0] == 0xAA) && (buf[1] == 0x55) )
	{
		index1 = (buf[5]<<24) | (buf[4]<<16) | (buf[3]<<8) | buf[2];
	}
	
	osal_memset(buf, 0, 6);
	HalFlashRead(BAUD_FLASH_ADDR2, buf, 6);
	if( (buf[0] == 0xAA) && (buf[1] == 0x55) )
	{
		index2 = (buf[5]<<24) | (buf[4]<<16) | (buf[3]<<8) | buf[2];
	}
	
	if(index1 > index2)
	{
		bank_curr_num = 0;
		addr = BAUD_FLASH_ADDR1;
	}
	else if(index2 > index1)
	{
		bank_curr_num = 1;
		addr = BAUD_FLASH_ADDR2;
	}
	else
	{
        osal_memset((uint8*)&user_data, 0, sizeof(user_data_t));
	}
	
	if(addr)
	{
		HalFlashRead(addr, (uint8*)&user_data, sizeof(user_data_t));
		
		if( user_data_sum(sizeof(user_data)-1, (uint8*)&user_data) != user_data.check_sum )
		{
			osal_memset((uint8*)&user_data, 0, sizeof(user_data_t));
		}
	}
    
    if(user_data.flg != 0x55AA)
    {
        user_data.flg = 0x55AA;
        user_data.index = 1;
        HalFlashRead(0x1000, (uint8*)&user_data.mac, sizeof(user_data.mac));
        osal_memcpy(user_data.name, DEVICE_NAME, ((osal_strlen((char *)DEVICE_NAME)>25)?25:osal_strlen((char *)DEVICE_NAME)));
        user_data_save();
    }
    LOG("baud:%d\r\n", baud_table[user_data.baud]);
    LOG("advin:%d\r\n", advin_table[user_data.advin]);
    LOG("connin:%d\r\n", connin_table[user_data.connin]);
    LOG("power:%d\r\n", user_data.power);
    LOG("name:%s\r\n", user_data.name);
    LOG("mac:%02X%02X%02X%02X%02X%02X\r\n", user_data.mac[5], \
                                            user_data.mac[4], \
                                            user_data.mac[3], \
                                            user_data.mac[2], \
                                            user_data.mac[1], \
                                            user_data.mac[0]);
}

/**
 * @brief user_data_save
 * 
 */
void user_data_save(void)
{
	uint32 addr = 0;
	
	bank_curr_num = !bank_curr_num;
	
	user_data.flg = 0x55AA;
	user_data.index++;
	
	user_data.check_sum = user_data_sum(sizeof(user_data)-1, (uint8*)&user_data);
	if(bank_curr_num)
	{
		addr = BAUD_FLASH_ADDR2;
	}
	else
	{
		addr = BAUD_FLASH_ADDR1;
	}
	
	HalFlashErase(addr>>12);
	HalFlashWrite(addr, (uint8*)&user_data, sizeof(user_data));
}
